package com.pig.graduate.service.impl.event;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pig.graduate.domain.event.EventExit;
import com.pig.graduate.domain.pigFarm.EventExitFarm;
import com.pig.graduate.domain.pigFarm.dto.EventExitFarmDTO;
import com.pig.graduate.mapper.event.EventExitMapper;
import com.pig.graduate.result.MyResult;
import com.pig.graduate.service.event.EventExitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author 星星眼
* @description 针对表【event_exit(离场)】的数据库操作Service实现
* @createDate 2023-03-05 14:49:24
*/
@Service
public class EventExitServiceImpl extends ServiceImpl<EventExitMapper, EventExit>
    implements EventExitService{

    @Autowired
    private EventExitMapper eventExitMapper;


    @Override
    public MyResult exitList(EventExit eventExit) {
        PageHelper.startPage(eventExit.getPageNum(), eventExit.getPageSize());
        List<EventExit> list = eventExitMapper.exitList(eventExit);
        for (EventExit exit : list) {
            String location = exit.getBuildingsName() + "-" + exit.getColumnName();
            exit.setLocation(location);
        }
        PageInfo<EventExit> pageInfo = new PageInfo<>(list);
        return  MyResult.success(pageInfo);
    }

    @Override
    public List<EventExitFarm> getExitFarm() {
        List<EventExitFarmDTO> dtoList = eventExitMapper.getExitFarm();
        Map<String, EventExitFarm> map = new HashMap<>();
        Integer totalNumber = eventExitMapper.getTotalNumber();
        for (EventExitFarmDTO eventExitFarmDTO : dtoList) {
            if(map.containsKey(eventExitFarmDTO)){
                if(eventExitFarmDTO.getPigType().equals(0)){ //种猪
                    if(eventExitFarmDTO.getBoarOrSow() == 0){ //母猪
                        map.get(eventExitFarmDTO.getName()).setSow(eventExitFarmDTO.getNumber());
                    }else{
                        map.get(eventExitFarmDTO.getName()).setBoar(eventExitFarmDTO.getNumber());
                    }
                }else{ //肉猪
                    map.get(eventExitFarmDTO.getName()).setPig(eventExitFarmDTO.getNumber());
                }
                map.get(eventExitFarmDTO.getName()).setTotal(map.get(eventExitFarmDTO.getName()).getTotal() + eventExitFarmDTO.getNumber());
            }else{
                EventExitFarm eventExitFarm = new EventExitFarm();
                if(eventExitFarmDTO.getPigType().equals(0)){ //种猪
                    if(eventExitFarmDTO.getBoarOrSow() == 0){ //母猪
                        eventExitFarm.setSow(eventExitFarmDTO.getNumber());
                    }else{
                        eventExitFarm.setBoar(eventExitFarmDTO.getNumber());
                    }
                }else{ //肉猪
                    eventExitFarm.setPig(eventExitFarmDTO.getNumber());
                }
                eventExitFarm.setName(eventExitFarmDTO.getName());
                eventExitFarm.setTotal(eventExitFarmDTO.getNumber());
                map.put(eventExitFarmDTO.getName(),eventExitFarm);
            }
        }
        map.values().forEach(eventExitFarm -> {
            Double number = (double)eventExitFarm.getTotal();
            double temp = number / totalNumber * 100;
            eventExitFarm.setPercentage(String.format("%.2f", temp)+"%");
        });
        List<EventExitFarm> reList = new ArrayList<>(map.values());
        return reList;
    }

}




